package 题目集.动态规划.最长递增子序列;

import org.junit.Test;

/**
 * https://leetcode.cn/problems/longest-strictly-increasing-or-strictly-decreasing-subarray/submissions/520855577/
 */
public class ch05_最长的严格递增或递减子数组 {
    public int longestMonotonicSubarray(int[] nums) {
        return Math.max(max(nums), min(nums));
    }

    public static int max(int[] nums) {
        int pre = 1;
        int res = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i - 1]) {
                pre++;
            } else {
                pre = 1;
            }
            res = Math.max(res, pre);
        }
        return res;
    }

    public static int min(int[] nums) {
        int pre = 1;
        int res = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] < nums[i - 1]) {
                pre++;
            } else {
                pre = 1;
            }
            res = Math.max(res, pre);
        }
        return res;
    }
    @Test
    public void test(){
        int[] nums={1,4,3,3,2};
        int res = longestMonotonicSubarray(nums);
        System.out.println(res);
    }
}
